package rank1400;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 1198. 找出所有行中最小公共元素
 */
public class No1198 {
    public int smallestCommonElement(int[][] mat) {
        List<Integer> ans = new ArrayList<>();
        for (int anInt : mat[0]) {
            ans.add(anInt);
        }
        for (int i = 1; i < mat.length; i++) {
            List<Integer> list = new ArrayList<>();
            for (int anInt : mat[i]) {
                list.add(anInt);
            }
            ans.retainAll(list);
        }
        Collections.sort(ans);
        return ans.isEmpty() ? -1 : ans.get(0);
    }
}
